package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.LinkedList;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.BigShootDetailActivity;
import quq.missq.activity.ImageViewPagerActivity;
import quq.missq.activity.TopicDetailActivity;
import quq.missq.beans.AtMeBean;
import quq.missq.beans.IJoinBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 7/27/15.
 */
public class IJoinCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_SUBSCRIBLE = 1;

    private Context mContext;
    private LinkedList<IJoinBean.Result> results;
    private LayoutInflater mInflater;


    public IJoinCommentAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<IJoinBean.Result> results){
        this.results = results;
    }

    public LinkedList<IJoinBean.Result> getData(){
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.foot_layout,null);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_SUBSCRIBLE){
            View view = mInflater.inflate(R.layout.i_join_comment_adapter,null);
            return new Holder(view);
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof Holder){
            Holder holder = (Holder)viewHolder;
            final IJoinBean.Result result = results.get(position);
            holder.avtar.setImageUrl(Constants.IMAGE_HOST+result.getComment().getAuthor().getAvatar80(),VolleyTool.getImageLoader());
            holder.name.setText(result.getComment().getAuthor().getNkname());
            holder.time.setText(result.getComment().getCreated());
            if(result.getComment() != null && result.getComment().getAttachs().size() > 0){
                holder.upImage.setVisibility(View.VISIBLE);
                ImageLoadUtil.loadImage(holder.upImage,
                        Constants.IMAGE_HOST+result.getComment().getAttachs().get(0).getThumbnail(),R.mipmap.empty_photo);
                holder.upImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, ImageViewPagerActivity.class);
                        intent.putExtra("position",0);
                        ArrayList<String> urls = new ArrayList<>();
                        urls.add(Constants.IMAGE_HOST+result.getComment().getAttachs().get(0).getOrigin());
                        intent.putExtra("netImageUrls",urls);
                        mContext.startActivity(intent);
                    }
                });
            }else{
                holder.upImage.setVisibility(View.GONE);
            }
            if(result.getReplyComment() != null){
                holder.content.setText("我的回复："+result.getComment().getContent());
                holder.tocomment.setVisibility(View.VISIBLE);
                holder.tocomment.setText(result.getReplyComment().getAuthor().getNkname()+"的评论: "+result.getReplyComment().getContent());
            }else{
                holder.content.setText("我评论的："+result.getComment().getContent());
                holder.tocomment.setVisibility(View.GONE);
            }
            holder.fname.setText(result.getPost().getAuthor().getNkname());
            holder.fcontent.setText(result.getPost().getTitle());
            if(result.getPost().getAttachs().size() > 0){
                holder.downImage.setVisibility(View.VISIBLE);
                ImageLoadUtil.loadImage(holder.downImage,
                        Constants.IMAGE_HOST+result.getPost().getAttachs().get(0).getThumbnail(),R.mipmap.empty_photo);
            }else{
                holder.downImage.setVisibility(View.GONE);
            }
            holder.detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    int category = result.getPost().getCategory().getId();
                    if(category == 1 || category == 2 || category == 3){
                        intent.putExtra("id",result.getPost().getId());
                        intent.setClass(mContext, BigShootDetailActivity.class);
                    }else{
                        intent.putExtra("id",result.getPost().getId());
                        intent.setClass(mContext, TopicDetailActivity.class);
                    }
                    mContext.startActivity(intent);
                }
            });
        }else if(viewHolder instanceof FootViewHolder){
            FootViewHolder footHolder = (FootViewHolder)viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_SUBSCRIBLE;
        }
    }

    @Override
    public int getItemCount() {
        return results.size()+1;
    }

    class Holder extends RecyclerView.ViewHolder{

        NetworkImageView avtar;
        TextView name;
        TextView time;
        TextView content;
        ImageView upImage;
        TextView tocomment;
        ImageView downImage;
        TextView fname;
        TextView fcontent;
        RelativeLayout detail;

        public Holder(View itemView) {
            super(itemView);
            avtar = (NetworkImageView)itemView.findViewById(R.id.avtar);
            name = (TextView)itemView.findViewById(R.id.name);
            time = (TextView)itemView.findViewById(R.id.time);
            content = (TextView)itemView.findViewById(R.id.content);
            upImage = (ImageView)itemView.findViewById(R.id.upImage);
            tocomment = (TextView)itemView.findViewById(R.id.tocomment);
            downImage = (ImageView)itemView.findViewById(R.id.downImage);
            fname = (TextView)itemView.findViewById(R.id.fname);
            fcontent = (TextView)itemView.findViewById(R.id.fcontent);
            detail = (RelativeLayout)itemView.findViewById(R.id.detail);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
